package com.xsmart.payment.dao;

import com.xsmart.common.entity.user.InfsUsers;
import com.xsmart.payment.domain.PayLog;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author chenzb
 * @create 2018-01-22 下午3:50
 **/
@Component
public interface PayDao {

    @Select("select * from pay_log where pay_code=#{payCode} ")
    Map<String,Object> findByPayCode(@Param("payCode") String payCode);

    @Select("select * from pay_log where log_id=#{logId} ")
    Map<String,Object> findByLogId(@Param("logId") String logId);

    @Insert("INSERT INTO pay_log(pay_id,pay_type_id,pay_code,order_id,order_amount,is_paid,pay_time,pay_expire_time,pay_status,remark) " +
            "values(#{payLog.payId},#{payLog.payTypeId},#{payLog.payCode},#{payLog.orderId},#{payLog.orderAmount}," +
            "#{payLog.isPaid},DATE_FORMAT(#{payLog.payTime},'%Y-%m-%d %H:%i:%S'),DATE_FORMAT(#{payLog.payExpireTime},'%Y-%m-%d %H:%i:%S')," +
            "#{payLog.payStatus},#{payLog.remark})")
    @Options(useGeneratedKeys = true, keyProperty = "payLog.logId",keyColumn = "log_id")
    int insertPayLog(@Param("payLog") PayLog payLog);


    @Insert("insert into pay_log_curr(log_id,order_id) values(#{logId},#{orderId})")
    int insertPayLogCurr(Map<String,Object> payLogCurrMap);

    @Delete("delete from pay_log_curr where order_id=#{orderId}")
    int deletePayLogCurr(@Param("orderId")Integer orderId);

    @Insert("insert into pay_log_weixin(log_id,order_id,prepay_id) values(#{logId},#{orderId},#{prepayId})")
    int insertPayLogWeixin(Map<String,Object> payLogWxMap);

    @Select("select * from INTU_WX_USER_INFO where openid=#{openid} ")
    Map<String,Object> findUserByOpenid(@Param("openid") String openid);

    @Insert("insert into intu_wx_user_info(user_id,openid) " +
            "values(#{userId},#{openid})")
    int insertIntuWxUserInfo(Map<String,Object> userExtMap);

    @Update({"update pay_log_weixin set transaction_id = #{transactionId},err_code=#{errCode},err_code_des=#{errCodeDes},response_xml = #{responseXml,jdbcType=CLOB}," +
            "pay_end_time=DATE_FORMAT(#{payEndTime},'%Y-%m-%d %H:%i:%S'),status_time=DATE_FORMAT(#{statusTime},'%Y-%m-%d %H:%i:%S') where log_id = #{logId}"})
    int updatePayLogWeixin(Map<String,Object> payLogWxMap);


    @Update({"update pay_log set pay_status = #{payStatus} where log_id = #{logId}"})
    int updatePayLog_PayStatus(Map<String,Object> payLogMap);

    @Select("select * from cfgs_sys_param where param_code=#{paramCode} and param_status = 1")
    @Cacheable(value = "sysParam")
    Map<String,Object> findByParamCode(@Param("paramCode") String paramCode);


    @Select("select * from payment where pay_code=#{payCode}")
    @Cacheable(value = "payment")
    Map<String,Object> findPaymentByPayCode(@Param("payCode") String payCode);

    @Select("select pl.* from pay_log pl left join pay_log_curr curr on curr.log_id = pl.log_id where curr.order_id=#{orderId}")
    Map<String,Object> findPayLogByOrderId(@Param("orderId") String orderId);

    @Select("select * from pay_log_curr where log_id=#{logId}")
    Map<String,Object> findPayLogCurrByLogId(@Param("logId") String logId);
}
